from contextlib import asynccontextmanager

from aioredis import from_url
from settings import Config


@asynccontextmanager
async def get_redis():
    """
    usage:
        async with get_redis() as redis:
            do some with redis
    """
    redis = await from_url(Config.REDIS_URL)
    try:
        yield redis
    finally:
        await redis.close()
